package top.fangw.common.mybatis.controller;

import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.fangw.common.core.result.Result;
import top.fangw.common.log.annotation.Log;

import java.util.List;

/**
 * @author 14325
 */

public abstract class BaseController<T,S extends IService<T>> {


    @Autowired
    protected S service;



    @Log
    @Operation(summary ="新增")
    @PostMapping("/add")
    public Result<?> add(@RequestBody T entity) {
        service.save(entity);
        return Result.success();
    }

    @Log
    @Operation(summary ="根据id删除")
    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam String id) {
        service.removeById(id);
        return Result.success();
    }

    @Log
    @Operation(summary ="根据id批量删除")
    @DeleteMapping("/deleteBatch")
    public Result<?> delete(@RequestParam List<String> ids) {
        service.removeBatchByIds(ids,200);
        return Result.success();
    }

    @Log
    @Operation(summary ="修改")
    @PutMapping("/update")
    public Result<?> update(@RequestBody T t) {
        service.updateById(t);
        return Result.success();
    }

    @Operation(summary ="根据id查询")
    @GetMapping("/detail")
    public Result<?> detail(@RequestParam String id) {
        T entity = service.getById(id);
        return Result.success(entity);
    }

}
